package com.wang.tasker.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FTPHelper {
	
	private FTPClient client;
	
	private String ip;
	private int port = 21;
	private String userName;
	private String password;
	
	public FTPHelper(){}
	
	public FTPHelper(String ip, String userName, String password){
		this.ip = ip;
		this.userName = userName;
		this.password = password;
	}

	public FTPClient getClient() {
		return client;
	}

	public void setClient(FTPClient client) {
		this.client = client;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean connect() throws Exception{
		if(client == null){
			client = new FTPClient();
			client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
	        int reply;
			client.setDefaultPort(this.port);
			client.connect(this.ip);
	        reply = client.getReplyCode();
	        if (!FTPReply.isPositiveCompletion(reply)) {
	        	client.disconnect();
	            throw new Exception("Exception in connecting to FTP Server");
	        }
			client.setDataTimeout(120000); 
			return client.login(this.userName, this.password);
		}
		return true;
	}

	public boolean download(String remoteFileName, String localFileName) {
        boolean flag = true; 
        BufferedOutputStream buffOut = null; 
        try { 
                buffOut = new BufferedOutputStream(new FileOutputStream(localFileName)); 
                flag = client.retrieveFile(remoteFileName, buffOut); 
        } catch (Exception e) { 
                e.printStackTrace(); 
                return false;
        } finally { 
                try { 
                        if (buffOut != null) 
                                buffOut.close(); 
                } catch (Exception e) { 
                        e.printStackTrace(); 
                } 
        } 
        return flag; 
		
	}
	
	public void uploadFile(String localFileFullName, String descFileFullName) throws Exception{
		InputStream input = new FileInputStream(new File(localFileFullName));
		this.client.storeFile(descFileFullName, input);
	}
	
	public void gotoPath(String path) throws IOException{
		this.client.changeWorkingDirectory(path);
	}
	
	public String[] getNameList(String exp) throws IOException {
		String files[] = client.listNames(exp);
		if (files == null || files.length == 0)
			return null;
		else {
			return files;
		}
	}
	
	public void disconnect(){
        if (this.client.isConnected()) {
            try {
                this.client.logout();
                this.client.disconnect();
            } catch (IOException f) {
                // do nothing as file is already saved to server
            }
        }
    }

}
